﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class MaintainDeptInfoDAL
    {
        StationeryInventoryEntities1 ent;

        public MaintainDeptInfoDAL()
        {
            ent = new StationeryInventoryEntities1();
        }

        public object getDepartmentCode(String userName)
        {
            object obj = new object();

            obj = (from e in ent.Employees
                   join u in ent.Users
                   on e.UserID equals u.UserID
                   where u.UserName == userName
                   select e.DeptID).SingleOrDefault();
            return obj;
        }

        public object getDepartmentName(String userName)
        {
            object obj = new object();

            var deptID = (from e in ent.Employees
                   join u in ent.Users
                   on e.UserID equals u.UserID
                   where u.UserName == userName
                   select e.DeptID).SingleOrDefault();
            obj = (from d in ent.Departments
                   where d.DeptID == deptID
                   select d.DeptName).SingleOrDefault();
            return obj;
        }

        public object getDepartmentHead(String userName)
        {
            object obj = new object();

            var deptID = (from e in ent.Employees
                          join u in ent.Users
                          on e.UserID equals u.UserID
                          where u.UserName == userName
                          select e.DeptID).SingleOrDefault();

            var deptHeadID = (from d in ent.Departments
                   where d.DeptID == deptID
                   select d.DeptHeadID).SingleOrDefault();
            obj = (from e in ent.Employees
                   where e.EmployeeID == deptHeadID
                   select e.EmployeeName).SingleOrDefault();
            return obj;
        }

        public object getRepresentativeName(String userName)
        {
            List<object> obj = new List<object>();

            //Department deptID = (from e in ent.Employees
            //              join u in ent.Users
            //              on e.UserID equals u.UserID
            //              where u.UserName == userName
            //              select e).SingleOrDefault();

            Employee emp = (from x in ent.Employees where x.User.UserName == userName select x).FirstOrDefault();

            Department deptID = (from x in ent.Departments where x.DeptHeadID == emp.EmployeeID select x).FirstOrDefault();

            //to be a rep, everybody has to have employee role and must not be acting manager and dept Head

            var empNameList = (from e in ent.Employees
                               where e.DeptID == deptID.DeptID
                               && (e.User.RoleID == 1
                               || e.User.RoleID == 2)
                               select e);
           

                    //&& e.EmployeeID != deptID.DeptHeadID
                    //&& e.EmployeeID != deptID.ActingManagerID
                   

            obj = empNameList.ToList<object>();
            return obj;
        }

        public object getUpdatedRepresentativeName(string userName)
        {
            object obj = new object();

            var deptID = (from e in ent.Employees
                          join u in ent.Users
                          on e.UserID equals u.UserID
                          where u.UserName == userName
                          select e.DeptID).SingleOrDefault();

            var repID = (from d in ent.Departments
                         where d.DeptID == deptID
                         select d.RepresentativeID).SingleOrDefault();
 
            obj = (from e in ent.Employees
                           where e.EmployeeID == repID
                           select e.EmployeeName).SingleOrDefault();
            return obj;
        }

        public object getCollectionPoint() {
            List<object> obj = new List<object>();

            var collPoint = (from c in ent.CollectionPoints select c);
            obj = collPoint.ToList<object>();
            return obj;
        }

        public object getUpdatedCollectionPoint(string userName)
        {
            object obj = new object();

            var deptID = (from e in ent.Employees
                          join u in ent.Users
                          on e.UserID equals u.UserID
                          where u.UserName == userName
                          select e.DeptID).SingleOrDefault();

            var collPID = (from d in ent.Departments
                         where d.DeptID == deptID
                         select d.CollectionPointID).SingleOrDefault();

            obj = (from c in ent.CollectionPoints
                   where c.CollectionPointID == collPID
                   select c.CollectionPointName).SingleOrDefault();

            return obj;
        }

        //public object getCollectionTime(String collectionID)
        //{
        //    int cId = Convert.ToInt32(collectionID);

        //    object obj = new object();

        //    obj = (from c in ent.CollectionPoints
        //           where c.CollectionPointID == cId
        //           select c.CollectionTime).FirstOrDefault();

        //    return obj;
        //}

        public object getCollectionTimeByCPID(int collPointID) {
            object obj = new object();
            
            obj = (from c in ent.CollectionPoints
                   where c.CollectionPointID == collPointID
                   select c.CollectionTime).SingleOrDefault();
 
            return obj;
        }

        public void addInfo(string deptCode, int repID, int collPointID) {
            try
            {
                Department currentDepart = new Department();
                currentDepart = (from d in ent.Departments where d.DeptID == deptCode select d).FirstOrDefault();

                if (currentDepart.RepresentativeID != null)
                {
                    int rId = (int)currentDepart.RepresentativeID;
                    Employee prevEmp = (from e in ent.Employees where e.EmployeeID == rId select e).FirstOrDefault();
                    User u = ent.Users.FirstOrDefault(x => x.UserID == prevEmp.UserID);
                    u.RoleID = 1;
                    ent.SaveChanges();
                }
               
                currentDepart.CollectionPointID = collPointID;
                currentDepart.RepresentativeID = repID;
                ent.SaveChanges();



                //change the role of the user to rep
                Employee emp = (from e in ent.Employees where e.EmployeeID == repID select e).FirstOrDefault();
                User user = (from u in ent.Users where u.UserID == emp.UserID select u).FirstOrDefault();
                user.RoleID = 2;
                ent.SaveChanges();



            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
    }
}

